package com.base.hsm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.hsm.domain.bo.TSysHsmBo;
import com.base.hsm.domain.vo.TSysHsmVo;
import com.base.hsm.service.ITSysHsmService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.hsm.domain.TSysHsm;
import com.base.hsm.mapper.TSysHsmMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 密码机信息Service业务层处理
 *
 * @author base
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class TSysHsmServiceImpl implements ITSysHsmService {

    private final TSysHsmMapper baseMapper;

    /**
     * 查询密码机信息
     */
    @Override
    public TSysHsmVo queryById(String hsmId){
        return baseMapper.selectVoById(hsmId);
    }

    /**
     * 查询密码机信息列表
     */
    @Override
    public TableDataInfo<TSysHsmVo> queryPageList(TSysHsmBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysHsm> lqw = buildQueryWrapper(bo);
        Page<TSysHsmVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询密码机信息列表
     */
    @Override
    public List<TSysHsmVo> queryList(TSysHsmBo bo) {
        LambdaQueryWrapper<TSysHsm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysHsm> buildQueryWrapper(TSysHsmBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysHsm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getHsmIp()), TSysHsm::getHsmIp, bo.getHsmIp());
        lqw.eq(StringUtils.isNotBlank(bo.getHsmPort()), TSysHsm::getHsmPort, bo.getHsmPort());
        lqw.eq(bo.getMainKeyIdx() != null, TSysHsm::getMainKeyIdx, bo.getMainKeyIdx());
        lqw.eq(bo.getDeviceKeyPairIdx() != null, TSysHsm::getDeviceKeyPairIdx, bo.getDeviceKeyPairIdx());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TSysHsm::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceyAuthCode()), TSysHsm::getPriceyAuthCode, bo.getPriceyAuthCode());
        return lqw;
    }

    /**
     * 新增密码机信息
     */
    @Override
    public Boolean insertByBo(TSysHsmBo bo) {
        TSysHsm add = BeanUtil.toBean(bo, TSysHsm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setHsmId(add.getHsmId());
        }
        return flag;
    }

    /**
     * 修改密码机信息
     */
    @Override
    public Boolean updateByBo(TSysHsmBo bo) {
        TSysHsm update = BeanUtil.toBean(bo, TSysHsm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysHsm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除密码机信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
